package com.ibm.moa.outmail.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebSettings;

import com.ibm.moa.outmail.utils.HtmlConverter;

@SuppressLint("NewApi")
public class MessageWebView extends RigidWebView {

	/**
	 * Check whether the single column layout algorithm can be used on this version of Android.
	 * 
	 * <p>
	 * Single column layout was broken on Android < 2.2 (see <a href="http://code.google.com/p/android/issues/detail?id=5024">issue 5024</a>).
	 * </p>
	 * 
	 * <p>
	 * Android versions >= 3.0 have problems with unclickable links when single column layout is enabled (see <a
	 * href="http://code.google.com/p/android/issues/detail?id=34886">issue 34886</a> in Android's bug tracker, and <a
	 * href="http://code.google.com/p/k9mail/issues/detail?id=3820">issue 3820</a> in K-9 Mail's bug tracker).
	 */
	public static boolean isSingleColumnLayoutSupported() {
		return (Build.VERSION.SDK_INT > 7 && Build.VERSION.SDK_INT < 11);
	}

	public MessageWebView(Context context) {
		super(context);
	}

	public MessageWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MessageWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	/**
	 * Configure a web view to load or not load network data. A <b>true</b> setting here means that network data will be blocked.
	 * 
	 * @param shouldBlockNetworkData
	 *            True if network data should be blocked, false to allow network data.
	 */
	public void blockNetworkData(final boolean shouldBlockNetworkData) {
		/*
		 * Block network loads.
		 * 
		 * Images with content: URIs will not be blocked, nor will network images that are already in the WebView cache.
		 */
		getSettings().setBlockNetworkLoads(shouldBlockNetworkData);
	}

	/**
	 * Configure a {@link android.webkit.WebView} to display a Message. This method takes into account a user's preferences when configuring the view.
	 * This message is used to view a message and to display a message being replied to.
	 */
	@SuppressWarnings("deprecation")
	public void configure() {
		this.setVerticalScrollBarEnabled(true);
		this.setVerticalScrollbarOverlay(true);
		this.setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY);
		this.setLongClickable(true);

		final WebSettings webSettings = this.getSettings();

		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

		webSettings.setSupportZoom(true);
		webSettings.setBuiltInZoomControls(true);
		webSettings.setUseWideViewPort(true);
		if (true) {
			webSettings.setLoadWithOverviewMode(true);
		}

		disableDisplayZoomControls();

		webSettings.setJavaScriptEnabled(false);
		webSettings.setLoadsImagesAutomatically(true);
		webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);

		if (isSingleColumnLayoutSupported()) {
			webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
		} else {
			webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		}

		disableOverscrolling();

		webSettings.setTextZoom(16);// TODO

		// Disable network images by default. This is overridden by preferences.
		blockNetworkData(true);
	}

	/**
	 * Disable on-screen zoom controls on devices that support zooming via pinch-to-zoom.
	 */
	@TargetApi(11)
	private void disableDisplayZoomControls() {
		if (Build.VERSION.SDK_INT >= 11) {
			PackageManager pm = getContext().getPackageManager();
			boolean supportsMultiTouch = pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)
					|| pm.hasSystemFeature(PackageManager.FEATURE_FAKETOUCH_MULTITOUCH_DISTINCT);

			getSettings().setDisplayZoomControls(!supportsMultiTouch);
		}
	}

	@TargetApi(9)
	private void disableOverscrolling() {
		if (Build.VERSION.SDK_INT >= 9) {
			setOverScrollMode(OVER_SCROLL_NEVER);
		}
	}

	/**
	 * Load a message body into a {@code MessageWebView}
	 * 
	 * <p>
	 * Before loading, the text is wrapped in an HTML header and footer so that it displays properly.
	 * </p>
	 * 
	 * @param text
	 *            The message body to display. Assumed to be MIME type text/html.
	 */
	public void setText(String text) {
		// Include a meta tag so the WebView will not use a fixed viewport width
		// of 980 px
		String content = "<html><head><meta name=\"viewport\" content=\"width=device-width\"/>";
		content += HtmlConverter.cssStylePre();
		content += "</head><body>" + text + "</body></html>";
		loadDataWithBaseURL("http://", content, "text/html", "utf-8", null);
		resumeTimers();
	}

	private boolean isScroll = true;// webview 是否滚动

	PointF downP = new PointF();
	/** 触摸时当前的点 **/
	PointF curP = new PointF();

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// return super.dispatchTouchEvent(ev);
		return true;
	}

	public boolean dispatchTouchEvent(MotionEvent ev, boolean isTouch) {
		if (isTouch) {
			return true;
		} else {
			return super.dispatchTouchEvent(ev);

		}
	}

}
